package insta.project.media;

import insta.project.player.Audio;

import java.util.ArrayList;

import android.content.ContentResolver;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.provider.MediaStore;
import android.util.Log;

public class MediaUtil {
	public static final String TAG = "MediaUtil";

	public static Cursor query(Context context, Uri uri, String[] projection,
			String selection, String[] selectionArgs, String sortOrder,
			int limit) {
		try {
			ContentResolver resolver = context.getContentResolver();
			if (resolver == null) {
				return null;
			}
			if (limit > 0) {
				uri = uri.buildUpon().appendQueryParameter("limit", "" + limit)
						.build();
			}
			return resolver.query(uri, projection, selection, selectionArgs,
					sortOrder);
		} catch (UnsupportedOperationException ex) {
			return null;
		}
	}

	public static Cursor query(Context context, Uri uri, String[] projection,
			String selection, String[] selectionArgs, String sortOrder) {
		return query(context, uri, projection, selection, selectionArgs,
				sortOrder, 0);
	}

	private static int idFromCursor(Cursor c) {
		int id = -1;
		if (c != null) {
			c.moveToFirst();
			if (!c.isAfterLast()) {
				id = c.getInt(0);
			}
		}
		c.close();
		return id;
	}

	public static int idForplaylist(Context context, String name) {
		Cursor c = query(context,
				MediaStore.Audio.Playlists.EXTERNAL_CONTENT_URI,
				new String[] { MediaStore.Audio.Playlists._ID },
				MediaStore.Audio.Playlists.NAME + " like '" + name + "'", null,
				MediaStore.Audio.Playlists.NAME);
		return idFromCursor(c);
	}

	public static int idFortrack(Context context, String path) {
		Cursor c = query(context,
				MediaStore.Audio.Playlists.EXTERNAL_CONTENT_URI,
				new String[] { MediaStore.Audio.Media._ID },
				MediaStore.Audio.Media.DATA + " like '" + path + "'", null,
				MediaStore.Audio.Media.DATA);
		return idFromCursor(c);
	}

	public static void removePlaylist(ContentResolver resolver, long playlistId) {
		Uri uri = MediaStore.Audio.Playlists.EXTERNAL_CONTENT_URI;
		resolver.delete(uri, MediaStore.Audio.Playlists._ID + " = '"
				+ playlistId + "'", null);
	}
	
	@SuppressWarnings("unused")
	private static int delete(ContentResolver contentResolver, Uri uri,
			String where) {
		return contentResolver.delete(uri, where, null);
	}

	public static void writePlaylist(Context context, String playlistName,
			ArrayList<Audio> paths) {

		ContentResolver resolver = context.getContentResolver();
		Uri uri;

		ContentValues values = new ContentValues(1);
		values.put(MediaStore.Audio.Playlists.NAME, playlistName);
		int size = paths.size();

		Log.d("paths_size", "" + size);

		int playlistId = idForplaylist(context, playlistName);
		Log.d("first_playlistid", "" + playlistId);

		if (playlistId > -1) {
			uri = MediaStore.Audio.Playlists.Members.getContentUri("external",
					playlistId);
			
			resolver.delete(uri, null, null);
			for (int k = 0; k < size; ++k) {
				values = putContentValues(context, paths, size, k, playlistId);
				resolver.insert(uri, values);
			}
		} else {
			uri = resolver.insert(
					MediaStore.Audio.Playlists.EXTERNAL_CONTENT_URI, values);

			playlistId = idForplaylist(context, playlistName);

			Log.d("second_playlistid", "" + playlistId);
			Log.d(TAG, String.format("Writing playlist %s", uri));

			uri = MediaStore.Audio.Playlists.Members.getContentUri("external",
					playlistId);

			for (int k = 0; k < size; ++k) {
				values = putContentValues(context, paths, size, k, playlistId);
				resolver.insert(uri, values);
			}
		}
	}

	private static ContentValues putContentValues(Context context,
			ArrayList<Audio> paths, int size, int k, int id) {

		ContentValues values;
		values = new ContentValues(size);
		Log.d("audio_id", "" + paths.get(k).getAudio_id());
		values.put(MediaStore.Audio.Playlists.Members.AUDIO_ID, paths.get(k)
				.getAudio_id());
		values.put(MediaStore.Audio.Playlists.Members.PLAY_ORDER, k);

		return values;
	}
}